package it;

import org.apache.log4j.Logger;
import org.junit.Test;

/**
 * @author Toan, Minh, Dang, Van, Gilles
 * 
 * This class provides integration tests for delete, merge and rename methods.
 * 
 * The cases are detailed in file below:
 * /src/test/resources/08112012_label_test_cases_full.xlsx
 * 
 * Before each test case is run, an xml file representing the test dataset is loaded in confluence.
 */

public class IntegrationTestCase extends AbstractIntegrationTest {

	private static Logger log = Logger.getLogger(IntegrationTestCase.class);

	/**
	 * Test Case 1: user_bruce-delete-in_all_contents--space_a
	 * Details about this test case in file referred in class javadoc 
	 */	
	@Test
	public void testCase01_delete() {

		log.info("TEST CASE 1: delete action user_bruce-delete-in_all_contents--space_a");
		String label = "in_all_contents";
		String spaceKey = "spacea";
		String labelCountInSpace = getLabelCount(label, spaceKey);
		String labelCountInAllSpace = getLabelCount(label, null);
		log.info("Before| global:" + labelCountInAllSpace + "| " + spaceKey	+ ":" + labelCountInSpace);
		
		assertEquals("6", labelCountInSpace);
		assertEquals("18", labelCountInAllSpace);
		
		// logout admin
		logout();
		// login with user_bruce
		login("user_bruce", "user_bruce");
		// process delete label in_all_contents in spacea
		gotoPage(getDeleteActionPath() + "?spaceKey=" + spaceKey + "&label=" + label + "&atl_token=" + getToken());
		
		// logout user_bruce to process the action
		logout();
		// login with admin to get result after test
		loginAsAdmin();
		
		labelCountInSpace = getLabelCount(label, spaceKey);
		labelCountInAllSpace = getLabelCount(label, null);
		log.info("After| global:" + labelCountInAllSpace + "| " + spaceKey + ":" + labelCountInSpace);
		
		assertEquals("0", labelCountInSpace);
		assertEquals("12", labelCountInAllSpace);

	}

	/**
	 * Test Case 2: user_bruce-delete-in_all_contents--all_spaces
	 * Details about this test case in file referred in class javadoc 
	 */
	@Test
	public void testCase02_delete() {

		log.info("TEST CASE 2: delete action user_bruce-delete-in_all_contents--all_spaces");
		String label = "in_all_contents";
		String spaceKey = "";
		String labelCountInSpace = getLabelCount(label, spaceKey);
		String labelCountInAllSpace = getLabelCount(label, null);
		log.info("Before| global:" + labelCountInAllSpace + "| " + spaceKey
				+ ":" + labelCountInSpace);
		assertEquals("18", labelCountInAllSpace);
		
		// logout admin
		logout();
		// login with user_bruce to process the action
		login("user_bruce", "user_bruce");
		
		// process delete label in_all_contents in spacea
		gotoPage(getDeleteActionPath() + "?spaceKey=" + spaceKey + "&label=" + label + "&atl_token=" + getToken());
		
		// logout user_bruce to process the action
		logout();
		// login with admin to get result after test
		loginAsAdmin();
		
		labelCountInAllSpace = getLabelCount(label, null);
		log.info("After| global:" + labelCountInAllSpace + "| " + spaceKey + ":" + labelCountInSpace);
		
		assertEquals("0", labelCountInAllSpace);		
	}

	/**
	 * Test Case 3: user_leon-delete-in_all_contents--space_a
	 * Details about this test case in file referred in class javadoc 
	 */	
	@Test
	public void testCase03_delete() {

		log.info("TEST CASE 3: delete action user_leon-delete-in_all_contents--space_a");
		String label = "in_all_contents";
		String spaceKey = "spacea";
		String labelCountInSpace = getLabelCount(label, spaceKey);
		String labelCountInAllSpace = getLabelCount(label, null);
		log.info("Before| global:" + labelCountInAllSpace + "| " + spaceKey	+ ":" + labelCountInSpace);
		
		assertEquals("6", labelCountInSpace);
		assertEquals("18", labelCountInAllSpace);
		
		// logout admin
		logout();
		// login with user_bruce
		login("user_leon", "user_leon");
		
		// process delete label in_all_contents in spacea
		gotoPage(getDeleteActionPath() + "?spaceKey=" + spaceKey + "&label="	+ label + "&atl_token=" + getToken());
		
		// logout user_bruce to process the action
		logout();
		// login with admin to get result after test
		loginAsAdmin();
		
		labelCountInSpace = getLabelCount(label, spaceKey);
		labelCountInAllSpace = getLabelCount(label, null);
		log.info("After| global:" + labelCountInAllSpace + "| " + spaceKey + ":" + labelCountInSpace);
		
		assertEquals("4", labelCountInSpace);
		assertEquals("16", labelCountInAllSpace);
	}

	/**
	 * Test Case 4: user_leon-delete-in_all_contents--all_spaces
	 * Details about this test case in file referred in class javadoc 
	 */
	@Test
	public void testCase04_delete() {

		log.info("TEST CASE 4: delete action user_leon-delete-in_all_contents--all_spaces");
		String label = "in_all_contents";
		String spaceKey = "";
		String labelCountInAllSpace = getLabelCount(label, null);
		log.info("Before| global:" + labelCountInAllSpace);
		
		assertEquals("18", labelCountInAllSpace);
		
		// logout admin
		logout();
		// login with user_bruce to process the action
		login("user_leon", "user_leon");
		
		// process delete label in_all_contents in spacea
		gotoPage(getDeleteActionPath() + "?spaceKey=" + spaceKey + "&label="	+ label + "&atl_token=" + getToken());
		
		// logout user_bruce
		logout();
		// login with admin to get result after test
		loginAsAdmin();
		
		labelCountInAllSpace = getLabelCount(label, null);
		log.info("After| global:" + labelCountInAllSpace);

		assertEquals("16", labelCountInAllSpace);
	}

	/**
	 * Test Case 5: user_bruce-rename-related-to-some_label-space_a
	 * Details about this test case in file referred in class javadoc 
	 */
	@Test
	public void testCase05_rename() {
		
		log.info("TEST CASE 5: rename action user_bruce-rename-related-to-some_label-space_a");
		String oldLabel = "related";
		String newLabel = "some_label";
		String spaceKey = "spacea";
		String oldLabelCountInSpace = getLabelCount(oldLabel, spaceKey);
		String oldLabelCountInAllSpaces = getLabelCount(oldLabel, null);
		log.info("Before| global:" + oldLabelCountInAllSpaces + "| " + spaceKey	+ ":" + oldLabelCountInSpace);
		
		assertEquals("4", oldLabelCountInSpace);
		assertEquals("6", oldLabelCountInAllSpaces);

		// logout admin
		logout();
		// login with user_bruce to process the action
		login("user_bruce", "user_bruce");
		
		gotoPage(getRenameActionPath() + "?spaceKey=" + spaceKey + "&oldLabel="
				+ oldLabel + "&newLabel=" + newLabel + "&atl_token="
				+ getToken());

		// logout user_bruce
		logout();
		// login with admin to get result after test
		loginAsAdmin();


		String newlabelCountInAllSpaces = getLabelCount(newLabel, null);
		oldLabelCountInAllSpaces = getLabelCount(oldLabel, null);
		log.info("After| global:" + newlabelCountInAllSpaces + "| " + ":" + oldLabelCountInAllSpaces);
		
		assertEquals("4", newlabelCountInAllSpaces);
		assertEquals("2", oldLabelCountInAllSpaces);

	}

	/**
	 * Test Case 6: user_bruce-rename-related-some_label-all_spaces
	 * Details about this test case in file referred in class javadoc 
	 */
	@Test
	public void testCase06_rename() {
		
		log.info("TEST CASE 6: rename action user_bruce-rename-related-some_label-all_spaces");
		String oldLabel = "related";
		String newLabel = "some_label";
		String spaceKey = "spacea";
		String oldLabelCountInSpace = getLabelCount(oldLabel, spaceKey);
		String oldLabelCountInAllSpaces = getLabelCount(oldLabel, null);
		log.info("Before| global:"
				+ oldLabelCountInAllSpaces
				+ "| "
				+ spaceKey
				+ ":"
				+ oldLabelCountInSpace);
		
		assertEquals("4", oldLabelCountInSpace);
		assertEquals("6", oldLabelCountInAllSpaces);

		// logout admin
		logout();
		// login with user_bruce to to process the action
		login("user_bruce", "user_bruce");
		
		// process rename action
		gotoPage(getRenameActionPath() + "?spaceKey=" + "&oldLabel=" + oldLabel
				+ "&newLabel=" + newLabel + "&atl_token=" + getToken());
		
		// logout user_bruce
		logout();
		// login with admin to get result after test
		loginAsAdmin();
		
		String newlabelCountInAllSpaces = getLabelCount(newLabel, null);
		oldLabelCountInAllSpaces = getLabelCount(oldLabel, null);
		log.info("After| global:" + newlabelCountInAllSpaces + "| " + ":" + oldLabelCountInAllSpaces);
		
		assertEquals("6", newlabelCountInAllSpaces);
		assertEquals("0", oldLabelCountInAllSpaces);

	}

	/**
	 * Test Case 7: user_leon-rename-related-some_label-space_a
	 * Details about this test case in file referred in class javadoc 
	 */
	@Test
	public void testCase07_rename() {
		
		log.info("TEST CASE 7: rename action user_leon-rename-related-some_label-space_a");
		String oldLabel = "related";
		String newLabel = "some_label";
		String spaceKey = "spacea";
		String oldLabelCountInAllSpaces = getLabelCount(oldLabel, null);
		log.info("Before| global:" + oldLabelCountInAllSpaces);
		
		assertEquals("6", oldLabelCountInAllSpaces);

		// logout admin
		logout();
		// login with user_leon to process the action
		login("user_leon", "user_leon");
		
		// process rename action
		gotoPage(getRenameActionPath() + "?spaceKey=" + spaceKey + "&oldLabel="
				+ oldLabel + "&newLabel=" + newLabel + "&atl_token="
				+ getToken());
		
		// logout user_leon
		logout();
		// login with admin to get result after test
		loginAsAdmin();
		
		String newlabelCountInAllSpaces = getLabelCount(newLabel, null);
		oldLabelCountInAllSpaces = getLabelCount(oldLabel, null);
		log.info("After| global:" + newlabelCountInAllSpaces + "| " + ":" + oldLabelCountInAllSpaces);
		
		assertEquals("2", newlabelCountInAllSpaces);
		assertEquals("4", oldLabelCountInAllSpaces);
		
	}

	/**
	 * Test Case 8: user_leon-rename-related-some_label-all_spaces
	 * Details about this test case in file referred in class javadoc 
	 */
	@Test
	public void testCase08_rename() {
		
		log.info("TEST CASE 8: rename action user_leon-rename-related-some_label-all_spaces");
		String oldLabel = "related";
		String newLabel = "some_label";
		String oldLabelCountInAllSpaces = getLabelCount(oldLabel, null);
		log.info("Before| global:" + oldLabelCountInAllSpaces);
		
		assertEquals("6", oldLabelCountInAllSpaces);

		// logout admin
		logout();
		// login with user_leon to process the action
		login("user_leon", "user_leon");
		
		// process rename action
		gotoPage(getRenameActionPath() + "?spaceKey=" + "&oldLabel=" + oldLabel + "&newLabel=" + newLabel + "&atl_token=" + getToken());
		
		// logout user_leon
		logout();
		// login with admin to get result after test
		loginAsAdmin();

		String newlabelCountInAllSpaces = getLabelCount(newLabel, null);
		oldLabelCountInAllSpaces = getLabelCount(oldLabel, null);
		log.info("After| global:" + newlabelCountInAllSpaces + "| " + ":" + oldLabelCountInAllSpaces);
		
		assertEquals("2", newlabelCountInAllSpaces);
		assertEquals("4", oldLabelCountInAllSpaces);
		
	}

	/**
	 * Test Case 9: action user_leon-rename-related-some_label-space_a
	 * Details about this test case in file referred in class javadoc 
	 */
	@Test
	public void testCase09_rename() {
		
		log.info("TEST CASE 9: rename action user_leon-rename-related-some_label-space_a");
		String oldLabel = "related";
		String newLabel = "some_label";
		String spaceKey = "spaceb";
		String oldLabelCountInAllSpaces = getLabelCount(oldLabel, null);
		log.info("Before| global:" + oldLabelCountInAllSpaces);
		
		assertEquals("6", oldLabelCountInAllSpaces);

		// logout admin
		logout();
		// login with user_leon to process the action
		login("user_leon", "user_leon");
		
		// process rename action
		gotoPage(getRenameActionPath() + "?spaceKey=" + spaceKey + "&oldLabel="
				+ oldLabel + "&newLabel=" + newLabel + "&atl_token="
				+ getToken());
		
		// logout user_leon
		logout();
		// login with admin to get result after test
		loginAsAdmin();
		
		String newlabelCountInAllSpaces = getLabelCount(newLabel, null);
		oldLabelCountInAllSpaces = getLabelCount(oldLabel, null);
		log.info("After| global:" + newlabelCountInAllSpaces + "| " + ":" + oldLabelCountInAllSpaces);
		
		assertEquals("6", oldLabelCountInAllSpaces);
	}

	/**
	 * Test Case 10: action user_bruce-merge-related-in_all_contents-space_a
	 * Details about this test case in file referred in class javadoc 
	 */
	@Test
	public void testCase10_merge() {

		log.info("TEST CASE 10: merge action user_bruce-merge-related-in_all_contents-space_a");
		String firstLabel = "related";
		String secondLabel = "in_all_contents";
		String spaceKey = "spacea";
		String labelCountInSpace = getLabelCount(firstLabel, spaceKey);
		String labelCountInAllSpace = getLabelCount(firstLabel, null);
		
		log.info("Before| global:" + labelCountInAllSpace + "| " + spaceKey + ":" + labelCountInSpace);
		
		assertEquals("4", labelCountInSpace);
		assertEquals("6", labelCountInAllSpace);
		
		// logout admin
		logout();
		// login with user_bruce to process the action
		login("user_bruce", "user_bruce");
		
		// process delete label in_all_contents in spacea
		gotoPage(getMergeActionPath() + "?spaceKey=" + spaceKey + "&firstLabel="
				+ firstLabel + "&secondLabel=" + secondLabel + "&atl_token="
				+ getToken());
		
		// logout user_bruce
		logout();
		// login with admin to get result after test
		loginAsAdmin();
		
		labelCountInSpace = getLabelCount(firstLabel, spaceKey);
		labelCountInAllSpace = getLabelCount(secondLabel, null);
		
		log.info("After| global:" + labelCountInAllSpace + "| " + spaceKey + ":" + labelCountInSpace);
		
		assertEquals("0", labelCountInSpace);
		assertEquals("18", labelCountInAllSpace);

	}

	/**
	 * Test Case 11: action user_bruce-merge-related-in_all_contents-all_spaces
	 * Details about this test case in file referred in class javadoc 
	 */
	@Test
	public void testCase11_merge() {

		log.info("TEST CASE 11: merge action user_bruce-merge-related-in_all_contents-all_spaces");
		String firstLabel = "related";
		String secondLabel = "in_all_contents";
		String spaceKey = "";
		String labelCountInAllSpace = getLabelCount(firstLabel, null);
		log.info("Before| global:" + labelCountInAllSpace + "| " + spaceKey	+ ":" + labelCountInAllSpace);
		
		assertEquals("6", labelCountInAllSpace);
		
		// logout admin
		logout();
		// login with user_bruce to process the action
		login("user_bruce", "user_bruce");
		
		// process delete label in_all_contents in spacea
		gotoPage(getMergeActionPath() + "?spaceKey=" + spaceKey + "&firstLabel="
				+ firstLabel + "&secondLabel=" + secondLabel + "&atl_token="
				+ getToken());
		
		// logout user_bruce
		logout();
		// login with admin to get result after test
		loginAsAdmin();
		
		String firstLabelCountInAllSpace = getLabelCount(firstLabel, null);
		String secondLabelCountInAllSpace = getLabelCount(secondLabel, null);
		
		log.info("After| global:" + firstLabelCountInAllSpace + "| " + spaceKey + ":" + secondLabelCountInAllSpace);

		assertEquals("0", firstLabelCountInAllSpace);
		assertEquals("18", secondLabelCountInAllSpace);
	}

	/**
	 * Test Case 12: action user_leon-merge-related-in_all_contents-space_a
	 * Details about this test case in file referred in class javadoc 
	 */
	@Test
	public void testCase12_merge() {

		log.info("TEST CASE 12: merge action user_leon-merge-related-in_all_contents-space_a");
		String firstLabel = "related";
		String secondLabel = "in_all_contents";
		String spaceKey = "spacea";
		String labelCountInAllSpace = getLabelCount(firstLabel, null);
		log.info("Before| global:" + labelCountInAllSpace + "| " + spaceKey	+ ":" + labelCountInAllSpace);
		
		assertEquals("6", labelCountInAllSpace);
		
		// logout admin
		logout();
		// login with user_leon to process action
		login("user_leon", "user_leon");
		
		// process delete label in_all_contents in spacea
		gotoPage(getMergeActionPath() + "?spaceKey=" + spaceKey + "&firstLabel="
				+ firstLabel + "&secondLabel=" + secondLabel + "&atl_token="
				+ getToken());
		
		// logout user_leon
		logout();
		// login with admin to get result after test
		loginAsAdmin();
		
		String firstLabelCountInAllSpace = getLabelCount(firstLabel, null);
		String secondLabelCountInAllSpace = getLabelCount(secondLabel, null);
		log.info("After| global:" + firstLabelCountInAllSpace + "| " + spaceKey	+ ":" + secondLabelCountInAllSpace);
		
		assertEquals("4", firstLabelCountInAllSpace);
		assertEquals("18", secondLabelCountInAllSpace);
	}

	/**
	 * Test Case 13: action user_leon-merge-related-in_all_contents-all_spaces
	 * Details about this test case in file referred in class javadoc 
	 */
	@Test
	public void testCase13_merge() {

		log.info("TEST CASE 13: merge action user_leon-merge-related-in_all_contents-all_spaces");
		String firstLabel = "related";
		String secondLabel = "in_all_contents";
		String spaceKey = "";
		String labelCountInAllSpace = getLabelCount(firstLabel, null);
		log.info("Before| global:" + labelCountInAllSpace + "| " + spaceKey
				+ ":" + labelCountInAllSpace);
		
		assertEquals("6", labelCountInAllSpace);
		
		// logout admin
		logout();
		// login with user_leon to process the action
		login("user_leon", "user_leon");
		
		// process delete label in_all_contents in spacea
		gotoPage(getMergeActionPath() + "?spaceKey="
				+ spaceKey + "&firstLabel=" + firstLabel + "&secondLabel="
				+ secondLabel + "&atl_token=" + getToken());
		
		// logout user_leon
		logout();
		// login with admin to get result after test
		loginAsAdmin();
		
		String firstLabelCountInAllSpace = getLabelCount(firstLabel, null);
		String secondLabelCountInAllSpace = getLabelCount(secondLabel, null);
		
		log.info("After| global:" + firstLabelCountInAllSpace + "| " + spaceKey	+ ":" + secondLabelCountInAllSpace);

		assertEquals("4", firstLabelCountInAllSpace);
		assertEquals("18", secondLabelCountInAllSpace);
	}

	/**
	 * Test Case 14: action user_leon-merge-label_b-in_all_contents-space_b
	 * Details about this test case in file referred in class javadoc 
	 */
	@Test
	public void testCase14_merge() {

		log.info("TEST CASE 14: merge action user_leon-merge-label_b-in_all_contents-space_b");
		String firstLabel = "label_b";
		String secondLabel = "in_all_contents";
		String spaceKey = "spaceb";
		String labelCountInAllSpace = getLabelCount(firstLabel, null);
		log.info("Before| global:" + labelCountInAllSpace + "| " + spaceKey	+ ":" + labelCountInAllSpace);
		
		assertEquals("6", labelCountInAllSpace);
		
		// logout admin
		logout();
		// login with user_leon to process the action
		login("user_leon", "user_leon");
		
		// process delete label in_all_contents in spacea
		gotoPage(getMergeActionPath() + "?spaceKey=" + spaceKey + "&firstLabel="
				+ firstLabel + "&secondLabel=" + secondLabel + "&atl_token="
				+ getToken());
		
		// logout user_leon
		logout();
		// login with admin to get result after test
		loginAsAdmin();
		
		String firstLabelCountInSpace = getLabelCount(firstLabel, spaceKey);
		String secondLabelCountInAllSpace = getLabelCount(secondLabel, null);
		log.info("After| global:" + firstLabelCountInSpace + "| " + spaceKey
				+ ":" + secondLabelCountInAllSpace);
		
		assertEquals("6", firstLabelCountInSpace);
	    assertEquals("18", secondLabelCountInAllSpace);
	}

	/**
	 * Test Case 15: action user_leon-merge-label_a-in_all_contents-all_spaces
	 * Details about this test case in file referred in class javadoc 
	 */
	@Test
	public void testCase15_merge() {

		log.info("TEST CASE 15: merge action user_leon-merge-label_a-in_all_contents-all_spaces");
		String firstLabel = "label_a";
		String secondLabel = "in_all_contents";
		String spaceKey = "";
		String labelCountInAllSpace = getLabelCount(firstLabel, null);
		log.info("Before| global:" + labelCountInAllSpace + "| " + spaceKey	+ ":" + labelCountInAllSpace);
		
		assertEquals("6", labelCountInAllSpace);
		
		// logout admin
		logout();
		// login with user_leon
		login("user_leon", "user_leon");
		
		// process delete label in_all_contents in spacea
		gotoPage(getMergeActionPath() + "?spaceKey="
				+ spaceKey + "&firstLabel=" + firstLabel + "&secondLabel="
				+ secondLabel + "&atl_token=" + getToken());
		
		// logout user_bruce
		logout();
		// login with admin to get result after test
		loginAsAdmin();
		
		String firstLabelCountInAllSpace = getLabelCount(firstLabel, spaceKey);
		String secondLabelCountInAllSpace = getLabelCount(secondLabel, null);
		log.info("After| global:" + firstLabelCountInAllSpace + "| " + spaceKey
				+ ":" + secondLabelCountInAllSpace);
		
		assertEquals("4", firstLabelCountInAllSpace);
		assertEquals("18", secondLabelCountInAllSpace);
	}

}
